using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.IO;
using CommonLib;
using FileSelector.Properties;

namespace FileSelector
{
    class Program
    {
        static string extensions = ".avi;.flv;.mpg;.mpeg;.mov;.3gp;.wav;.mp3;.ogg;.aac;.m4a;";

        static void Main(string[] args)
        {
            string path;
            if (args.Length == 1)
            {
                path = PathEncoder.Decode(args[0]);
            }
            else
            {
                path = Properties.Settings.Default.ROOTDIR;
            }

            Console.WriteLine(Resources.HttpHeader);
            Console.WriteLine();
            Console.WriteLine(Resources.HtmlHeader, path, PathConf.curl);
            string[] dirs = Directory.GetDirectories(path);
            if (Path.GetDirectoryName(path) != null)
                Console.WriteLine(Resources.DirItem, PathEncoder.Encode(Path.GetDirectoryName(path)), "..", PathConf.curl);
            foreach (string dir in dirs)
            {
                string name = Path.GetFileName(dir);
                Console.WriteLine(Resources.DirItem, PathEncoder.Encode(dir), name, PathConf.curl);
            }

            string[] files = Directory.GetFiles(path);
            string[] exts = extensions.Split(';');
            bool bOkExt = false;
            foreach (string file in files)
            {
                bOkExt = false;
                foreach (string ext in exts)
                {
                    if (Path.GetExtension(file).ToLower() == ext)
                    {
                        bOkExt = true;
                        break;
                    }
                }
                if (bOkExt == false) continue;

                string name = Path.GetFileName(file);
                Console.WriteLine(Resources.FileItem, PathEncoder.Encode(file), name, PathConf.curl);
            }
        }
    }
}
